package com.netty.client;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.OutputStream;
import java.net.Socket;

/**
 * @description:
 * @date 2020-04-01 14:09
 * @author: liujinhu
 */
public class NettyClient1 {

    public NettyClient1() throws Exception {
//        Socket socket = new Socket("localhost", 8888);
//        DatagramSocket datagramSocket = new DatagramSocket(8888, InetAddress.getLocalHost());
//        while (true) {
//
//            byte[] buf = new byte[1024];
//            DatagramPacket dp = new DatagramPacket(buf, buf.length);
//            System.out.println("开始读数据...");
//            datagramSocket.receive(dp);
//            String data = new String(dp.getData(), "GBK");
//            System.out.println(data);
//        }


        Socket socket = new Socket("localhost", 8888);
        OutputStream os = new BufferedOutputStream(socket.getOutputStream());
//        DataInputStream bis = new DataInputStream(socket.getInputStream());

        BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
        while (true){
//            os.write("asf".getBytes());
//            os.flush();
            if(bis.available()>0) {
                byte[] buf = new byte[bis.available()];
                System.out.println("开始读数据11...");
                int count = bis.read(buf);
                System.out.println("读取数据数量:" + count);
                System.out.println(new String(buf, "UTF-8"));
            }else{
                System.out.println("无消息");
            }

            Thread.sleep(5000);
        }



    }

    public static void main(String[] args) {
        try {
            new NettyClient1();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }



}
